package cn.iocoder.an.module.mold.controller.admin.repaircategory;

import cn.iocoder.an.module.mold.dal.dataobject.account.AccountDO;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.an.framework.common.pojo.PageParam;
import cn.iocoder.an.framework.common.pojo.PageResult;
import cn.iocoder.an.framework.common.pojo.CommonResult;
import cn.iocoder.an.framework.common.util.object.BeanUtils;
import static cn.iocoder.an.framework.common.pojo.CommonResult.success;

import cn.iocoder.an.framework.excel.core.util.ExcelUtils;

import cn.iocoder.an.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.an.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.an.module.mold.controller.admin.repaircategory.vo.*;
import cn.iocoder.an.module.mold.dal.dataobject.repaircategory.RepairCategoryDO;
import cn.iocoder.an.module.mold.service.repaircategory.RepairCategoryService;

@Tag(name = "管理后台 - 模具维修分类")
@RestController
@RequestMapping("/mold/repair-category")
@Validated
public class RepairCategoryController {

    @Resource
    private RepairCategoryService repairCategoryService;

    @PostMapping("/create")
    @Operation(summary = "创建模具维修分类")
    @PreAuthorize("@ss.hasPermission('mold:repair-category:create')")
    public CommonResult<Long> createRepairCategory(@Valid @RequestBody RepairCategorySaveReqVO createReqVO) {
        return success(repairCategoryService.createRepairCategory(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新模具维修分类")
    @PreAuthorize("@ss.hasPermission('mold:repair-category:update')")
    public CommonResult<Boolean> updateRepairCategory(@Valid @RequestBody RepairCategorySaveReqVO updateReqVO) {
        repairCategoryService.updateRepairCategory(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除模具维修分类")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mold:repair-category:delete')")
    public CommonResult<Boolean> deleteRepairCategory(@RequestParam("id") Long id) {
        repairCategoryService.deleteRepairCategory(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除模具维修分类")
                @PreAuthorize("@ss.hasPermission('mold:repair-category:delete')")
    public CommonResult<Boolean> deleteRepairCategoryList(@RequestParam("ids") List<Long> ids) {
        repairCategoryService.deleteRepairCategoryListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得模具维修分类")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('mold:repair-category:query')")
    public CommonResult<RepairCategoryRespVO> getRepairCategory(@RequestParam("id") Long id) {
        RepairCategoryDO repairCategory = repairCategoryService.getRepairCategory(id);
        return success(BeanUtils.toBean(repairCategory, RepairCategoryRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得模具维修分类分页")
    @PreAuthorize("@ss.hasPermission('mold:repair-category:query')")
    public CommonResult<PageResult<RepairCategoryRespVO>> getRepairCategoryPage(@Valid RepairCategoryPageReqVO pageReqVO) {
        PageResult<RepairCategoryDO> pageResult = repairCategoryService.getRepairCategoryPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, RepairCategoryRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出模具维修分类 Excel")
    @PreAuthorize("@ss.hasPermission('mold:repair-category:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportRepairCategoryExcel(@Valid RepairCategoryPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<RepairCategoryDO> list = repairCategoryService.getRepairCategoryPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "模具维修分类.xls", "数据", RepairCategoryRespVO.class,
                        BeanUtils.toBean(list, RepairCategoryRespVO.class));
    }

    @GetMapping("/all")
    @Operation(summary = "获得所有维修分类")
    public CommonResult<List<RepairCategoryDO>> getCategoryAll() {
        return success(repairCategoryService.getCategoryAll());
    }


}